<?php
namespace app\admin\controller;

use app\BaseController;
use think\facade\Db;
use think\facade\Session;
use think\facade\View;

class Index extends BaseController
{
    
    public function index(){
        if(Session::has('usession')){
            return View::fetch();
        }else{
            header('Location:http://tptest.com/admin/login');
        }
    }
    public function welCome(){
        return View::fetch('welcome');
    }
    //用户界面
    public function adminList(){
        $shu=[
            'username'=>'',
            'phone'=>'',
            'states'=>0
        ];
        //分页查询数据strpos() expects parameter 1 to be string, null given
        $data=Db::name('users')
            ->alias('b')
            ->join(['role'=>'r'],'b.urole = r.id','left')
            ->where('isdel',0)
            ->order('uid desc');
        $number=$data->count();
        $date=$data->paginate(2);
        $page=$date->render();
        //映射到html数据
        View::assign('shu',$shu);
        View::assign('number',$number);
        View::assign('data',$date);
        View::assign('page',$page);
        return View::fetch();
    }
    public function adminAdd(){
        return View::fetch();
    }
    //用户添加
    public function do_AdminAdd(){
        $uname=input('uname');
        //获取ajax传值转换成数组
        $data=[
            'uname'=>input('uname'),
            'upassword'=>md5(input('upassword')),
            'utellphone'=>input('utellphone'),
            'uemail'=>input('uemail'),
            'urole'=>input('urole'),
            'userphoto'=>input('userphoto')
        ];
        //进行用户名查重
        $vac=Db::name('users')->where('uname',$uname)->find();
        if(empty($vac)){
            //用户添加insert
            $add =Db::name('users')->replace()->insert($data);
            if($add){
                return json(['status'=>2000,'tips'=>'用户已添加！']);
            }else{
                return json(['status'=>2002,'tips'=>'用户添加失败！']);
            }

        }else{
            return json(['status'=>2001,'tips'=>'该用户名已存在！']);;
        }
    }
    //用户删除
    public function do_AdminDel(){
        $id=input('did');
        $del=Db::name('users')->where('uid',$id)->update(['isdel'=>1]);
        if($del){
            return json(['status'=>3000,'tips'=>'删除成功']);
        }else{
            return json(['status'=>3001,'tips'=>'删除失败']);
        }
    }
    //用户编辑
    public function adminEdit(){
        $id=input('id');
        $data=Db::name('users')->where('uid',$id)->find();
        View::assign('data',$data);
        return View::fetch();
    }
    //管理员修改提交
    public function do_AdminModify(){
        $data=[
            'uname'=>input('uname'),
            'upassword'=>md5(input('upassword')),
            'utellphone'=>input('utellphone'),
            'uemail'=>input('uemail'),
            'urole'=>input('urole')
        ];
        $update=Db::name('users')->where('uid', input('id'))->update($data);
        if($update){
            return json(['status'=>4000,'tips'=>'编辑成功！']);
        }else{
            return json(['status'=>4001,'tips'=>'编辑失败！']);
        }
    }
    public function do_AdminState(){
        $data=[
            'uid'=>input('id'),
            'states'=>input('state')
        ];
        $state=Db::name('users')->update($data);
        if($state){
            return json(['states'=>1000,'tips'=>'修改成功','stat'=>input('state')]);
        }else{
            return json(['states'=>1001,'tips'=>'修改失败']);
        }
    }
    public function do_query(){
        $username=input('username');
        $phone=input('utellphone');
        $states=input('states');
        $shu=[
           'username'=>$username,
            'phone'=>$phone,
            'states'=>$states
        ];
        //分页查询数据
        $data=Db::name('users')
            ->alias('b')
            ->join(['role'=>'r'],'b.urole = r.id','left')
            ->where('isdel',0)
            ->whereLike('uname',"%$username%")
            ->whereLike('utellphone',"%$phone%")
            ->whereLike('states',"%$states%")
            ->order('uid desc');
        $number=$data->count();
        $date=$data->paginate(2);
        $page=$date->render();
        //映射到html数据
        View::assign('number',$number);
        View::assign('data',$date);
        View::assign('page',$page);
        return View::fetch('admin_list');
    }
    //上传图片
    public function userphoto(){
        $file = request()->file('file');
        // 上传到本地服务器
        $savename = \think\facade\Filesystem::putFile( 'topic', $file);
        $tips=[
          'photo'=>$savename
        ];
        return json($tips);
    }
    //文章类型分类列表
    public function articletype_list(){
        $typename=input('typename');
        $date=Db::name('cate')
            ->alias('c1')
            ->join(['cate'=>'c2'] ,'c1.id=c2.pid','right')
            ->where('c2.isdeleted',0)
            ->whereLike('c2.catename',"%$typename%")
            ->order('c2.id','desc')
            ->field('c2.catename,c2.id,c2.pid,c2.pubdate,c1.catename as cname,c2.status');
        $count=$date->count();
        $data=$date->paginate(4);
        $page=$data->render();
        //映射
        View::assign('sel',$typename);
        View::assign('page',$page);
        View::assign('data',$data);
        return View::fetch();
    }
    //文章类型分类列表添加查询父级分类
    public function articletype_add(){
        $data=Db::name('cate')->order('pid','asc')->order('id','asc')->where('isdeleted','0')->select();
        View::assign('date',$data);
        return View::fetch();
    }
    //文章类型分类添加
    public function do_articletype_add(){
        $data=[
            'catename'=>input('name'),
            'pid'=>input('pid'),
            'status'=>input('status')
        ];
        $add=Db::name('cate')->insert($data);
        if($add){
            return json(['status'=>1000,'msg'=>'添加成功']);
        }else{
            return json(['status'=>1001,'msg'=>'添加失败']);
        }
    }
    //文章类型分类修改状态
    public function do_articlestatu(){
        $id=input('id');
        $status=['status'=>input('state')];
        $update=Db::name('cate')->where('id',$id)->update($status);
        if($update){
            return json(['status'=>1000,'tips'=>'成功','stat'=>input('state')]);
        }else{
            return json(['status'=>1001,'tips'=>'失败']);
        }
    }
    //文章类型分类伪删除
    public function do_delarticle(){
        $up=['isdeleted'=>1];
        $update=Db::name('cate')->where('id',input('id'))->update($up);
        if($update){
            return json(['status'=>1000,'tips'=>'成功']);
        }else{
            return json(['status'=>1001,'tips'=>'失败']);
        }
    }
    //文章类型分类修改查询当前分类父级名称以及自己名称
    public function articletypeupdate(){
        $data=Db::name('cate')->where('pid','0')->where('isdeleted','0')->select();
        $up=Db::name('cate')->where('id',input('id'))->find();
        View::assign('name',$up);
        View::assign('date',$data);
        return View::fetch('articletype_update');
    }
    //进行分类修改
    public function do_articletype_update(){
        $data=[
            'id'=>input('id'),
            'catename'=>input('name'),
            'pid'=>input('pid'),
            'status'=>input('status')
        ];
        $update=Db::name('cate')->update($data);
        if($update){
            return json(['status'=>1000,'tips'=>'成功']);
        }else{
            return json(['status'=>1001,'tips'=>'失败']);
        }
    }
    //文章列表
    public function article_list(){
        $sql=Db::name('article')
            ->alias('a')
            ->join(['cate'=>'c'],'a.cateid = c.id','left')
            ->field('a.id,c.catename,a.title,a.keywords,a.author,a.pubdate,a.istop,a.status,recommend')
            ->order('a.id','desc')
            ->where('isdeleteed',1);
        $count=$sql->count();
        $date=$sql->paginate(5);
        $page=$date->render();
        View::assign('count',$count);
        View::assign('sel',$date);
        View::assign('page',$page);
        return View::fetch();
    }
    //文章添加列表
    public function article_add(){
        $sel=Db::name('cate')
            ->where('isdeleted',0)
            ->select();
        View::assign('sel',$sel);
        return View::fetch();
    }
    //文章缩略图
    public function authorphoto(){
        $file = request()->file('file');
        // 上传到本地服务器
        $savename = \think\facade\Filesystem::putFile( 'thumb', $file);
        $tips=[
            'photo'=>$savename
        ];
        return json($tips);
    }
    //文章插入
    public function do_article_add(){
        $news=[
            'title'=>input('title'),
            'thumb'=>input('thumb'),
            'keywords'=>input('keywords'),
            'cateid'=>input('cateid'),
            'description'=>input('description'),
            'author'=>input('author'),
            'content'=>input('content'),
            'istop'=>input('istop'),
            'status'=>input('status')
        ];
        $insert=Db::name('article')->insert($news);
        if($insert){
            return json(['status'=>1,'tips'=>'上传成功']);
        }else{
            return json(['status'=>2,'tips'=>'上传失败']);
        }
    }
    //文章列表是否置顶以及状态修改
    public function do_modify(){
        $date=[
            'id'=>input('id'),
            input('dist')=>input('status')
        ];
        $update=Db::name('article')->update($date);
        if($update){
            if(input('dist')=='istop'){
                return json(['status'=>1,'tips'=>'置顶修改成功']);
            }else{
                return json(['status'=>1,'tips'=>'状态修改成功']);
            }
        }else{
            if(input('dist')=='istop'){
                return json(['status'=>0,'tips'=>'置顶修改失败']);
            }else{
                return json(['status'=>0,'tips'=>'状态修改失败']);
            }
        }
    }
    //文章删除
    public function do_article_del(){
        $date=[
            'id'=>input('id'),
            'isdeleteed'=>input('isdeleteed')
        ];
        $del=Db::name('article')->update($date);
        if($del){
            return json(['status'=>0,'tips'=>'删除成功']);
        }else{
            return json(['status'=>1,'tips'=>'删除失败']);
        }
    }
    //文章修改
    public function articleupdate(){
        $id=input('id');
        $option=Db::name('cate')->where('isdeleted',0)->select();
        $date=Db::name('article')->where('id',$id)->find();
        View::assign('option',$option);
        View::assign('date',$date);
        return View::fetch('article_update');
    }
    public function do_article_update(){
        $news=[
            'id'=>input('id'),
            'title'=>input('title'),
            'thumb'=>input('thumb'),
            'keywords'=>input('keywords'),
            'cateid'=>input('cateid'),
            'description'=>input('description'),
            'author'=>input('author'),
            'content'=>input('content'),
            'istop'=>input('istop'),
            'status'=>input('status')
        ];
        $upda=Db::name('article')->update($news);
        if($upda){
            return json(['status'=>1,'tips'=>'修改成功']);
        }else{
            return json(['status'=>0,'tips'=>'修改失败']);
        }
    }
    public function selarticle(){

    }
    public function artiimg(){
        $file = request()->file('file');
        // 上传到本地服务器
        $savename = \think\facade\Filesystem::putFile( 'articleimg', $file);
        $tips=[
            'code'=>0,
            'msg'=>'成功',
            'data'=>[
                'src'=>'/storage/'.$savename
            ]
        ];
        return json($tips);
    }
    //退出登录
    public function signout(){
        Session::delete('usession');
        $sess=Session::has('usession');
        if(!$sess){
            return json(['status'=>1,'info'=>'退出成功']);
        }else{
            return json(['status'=>2,'info'=>'退出失败']);
        }
    }
}